package com.kejiguan.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;


import com.kejiguan.ui.adapter.NeItemRunDataMemuAdapter;
import com.kejiguan.ui.model.DisplayArea;
import com.kejiguan.ui.model.ExhibitionRunData;
import com.kejiguan.ui.presenter.NeItemRunDataPresenter;
import com.kejiguan.ui.response.NeItemRunDataResponse;
import com.kejiguan.ui.util.CacheDataUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import lecho.lib.hellocharts.listener.ColumnChartOnValueSelectListener;
import lecho.lib.hellocharts.model.Column;
import lecho.lib.hellocharts.model.ColumnChartData;
import lecho.lib.hellocharts.model.SubcolumnValue;
import lecho.lib.hellocharts.util.ChartUtils;
import lecho.lib.hellocharts.view.ColumnChartView;

/**
 * Created by YYY on 16/7/24.
 */
public class FragmentDataStatistics extends Fragment implements View.OnClickListener{

    private View rootView;

    private RecyclerView displayAreaList;
    private NeItemRunDataMemuAdapter menuAdapter;

    private ColumnChartView chartView;
    private ColumnChartData data;

    private int currentPosition = 0;
    private List<ExhibitionRunData> neItemRunDatas;


    private NeItemRunDataPresenter mPresenter;


    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        EventBus.getDefault().register(this);
        mPresenter = new NeItemRunDataPresenter();
        mPresenter.attach(getContext());
        neItemRunDatas = new ArrayList<>();
        for (DisplayArea displayArea : CacheDataUtil.getInstance().getDisplayAreas()) {
            ExhibitionRunData neItemRunData = new ExhibitionRunData(displayArea.getDisplayId(), displayArea.getDisplayName());
            neItemRunDatas.add(neItemRunData);
        }
    }

    @Override
    public final View onCreateView(LayoutInflater inflater, ViewGroup container,
                                   Bundle savedInstanceState) {



        if (rootView == null) {

            rootView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_data_statistics_layout, container, false);
            chartView= (ColumnChartView) rootView.findViewById(R.id.chart);
            chartView.setOnValueTouchListener(new ValueTouchListener());
            displayAreaList = (RecyclerView) rootView.findViewById(R.id.display_menu_list);


            LinearLayoutManager displayAreaLayoutManager = new LinearLayoutManager(getContext());
            displayAreaLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            displayAreaList.setLayoutManager(displayAreaLayoutManager);
            menuAdapter = new NeItemRunDataMemuAdapter(getContext(), neItemRunDatas);
            menuAdapter.setOnClickListener(this);
            displayAreaList.setAdapter(menuAdapter);

            mPresenter.getNeItemRunData(getContext(), neItemRunDatas.get(currentPosition).getDisplayName(), neItemRunDatas.get(currentPosition).getDisplayId());


        }

        return rootView;
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){


            case R.id.menu_name:
                int position = (int) v.getTag(R.id.recyclerview_item_position);
                currentPosition = position;
                menuAdapter.setSelectArea(position);
                buildCharData(neItemRunDatas.get(currentPosition));
                mPresenter.getNeItemRunData(getContext(), neItemRunDatas.get(currentPosition).getDisplayName(), neItemRunDatas.get(currentPosition).getDisplayId());
                break;

        }


    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(NeItemRunDataResponse response) {

        if ("0000".equals(response.getRetCode())) {
            int menuSize = neItemRunDatas.size();
            for (int i = 0; i < menuSize; i++) {
                if (response.getDisplayId().equals(neItemRunDatas.get(i).getDisplayId())) {
                    neItemRunDatas.get(i).getNeItemRunItems().clear();
                    neItemRunDatas.get(i).getNeItemRunItems().addAll(response.getNeItemRunItems());
                    buildCharData(neItemRunDatas.get(i));
                    break;
                }
            }
        }
    }

    private void buildCharData(ExhibitionRunData exhibitionRunData)
    {
        int numColumns = 0;
        if (null==exhibitionRunData||null==exhibitionRunData.getNeItemRunItems())
        {
            numColumns=0;
        }else {
            numColumns=exhibitionRunData.getNeItemRunItems().size();
        }


        List<Column> columns = new ArrayList<Column>();
        List<SubcolumnValue> values;

        for (int i = 0; i < numColumns; ++i) {

            values = new ArrayList<SubcolumnValue>();

            values.add(new SubcolumnValue((float) Math.random() * 20f, ChartUtils.pickColor()));

            Column column = new Column(values);
            column.setHasLabels(true);
            column.setHasLabelsOnlyForSelected(true);
            columns.add(column);
        }

        data = new ColumnChartData(columns);

        data.setAxisXBottom(null);
        data.setAxisYLeft(null);

        chartView.setColumnChartData(data);

    }


    private class ValueTouchListener implements ColumnChartOnValueSelectListener {

        @Override
        public void onValueSelected(int columnIndex, int subcolumnIndex, SubcolumnValue value) {
            Toast.makeText(getActivity(), "Selected: " + value, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onValueDeselected() {
            // TODO Auto-generated method stub

        }

    }


    private int getSign() {
        int[] sign = new int[]{-1, 1};
        return sign[Math.round((float) Math.random())];
    }


}
